<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" :width="600">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="70px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="备料复核" prop="materialCheck">
            <el-input v-model="formData.materialCheck" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发料" prop="issuMaterials">
            <el-input v-model="formData.issuMaterials" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formData.remark" clearable placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- <el-divider /> -->
    <el-table border v-loading="loading" :data="list">
        <el-table-column type="index" label="序号" align="center" width="55" />
        <el-table-column label="箱码" align="center" prop="cartonCode" />
        <el-table-column label="数量" align="center" prop="num" />
        <!-- <el-table-column label="操作" align="center">
          <template #default="scope">
            <div class="flex items-center justify-center">
              <el-button
              v-hasPermi="['system:menu:delete']"
              link
              type="danger"
              @click="handleDelete(scope.row)"
            >
              清除箱码
            </el-button>
            </div>
          </template>
        </el-table-column> -->
      </el-table>

    <template #footer>
      <el-button :disabled="isDisabled" type="primary" @click="submitForm" ref="subBtn">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
  import * as materialPickApi from '@/api/hcxly/materialPick/Index'
  import { FormRules } from 'element-plus'

  defineOptions({ name: 'materialPickCheckForm' });

  const { t } = useI18n() // 国际化
  const message = useMessage() // 消息弹窗

  const dialogVisible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
  const formType = ref('') // 表单的类型：create - 新增；update - 修改
  const formData = ref({
    id: '',
    orderCode: '',
    type: '',
    code: '',
    specification: '',
    materialCheck: '',
    issuMaterials: '',
    remark: '',
    materialStatus: ''
  })

  //打开弹窗
  const open = async (row) => {
    console.log(row)
    dialogVisible.value = true
    dialogTitle.value = t('修改')
    formData.value.id = row.id
    formData.value.materialCheck = row.materialCheck
    formData.value.issuMaterials = row.issuMaterials
    formData.value.remark = row.remark
    // formData.value.orderCode = row.orderCode
    // formData.value.type = row.type
    // formData.value.code = row.code
    // formData.value.materialStatus = row.materialStatus
    getList(row.id)
  }
  defineExpose({ open }) // 提供 open 方法，用于打开弹窗

  //表单验证
  const formRules = reactive<FormRules>({
    // name: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
    // ip: [{ required: true, message: '轻重不能为空', trigger: 'blur' }],
    // port: [{ required: true, message: '裁剪尺寸不能为空', trigger: 'blur' }],
  })

  const formRef = ref() // 表单 Ref
  /** 提交表单 */
  const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
  const submitForm = async () => {
    console.log(formData.value)
    // 校验表单
    if (!formRef) return
    const valid = await formRef.value.validate()
    if (!valid) return
    // 提交请求
    formLoading.value = true
    try {
      const data = formData.value as unknown as materialPickApi.materialPickVO
      console.log(data)
      await materialPickApi.materialPickCheck(data)
      message.success(t('审核成功'))
      dialogVisible.value = false
      // 发送操作成功的事件
      emit('success')

      // resetForm()
    } finally {
      formLoading.value = false
    }
  }

  /** 重置表单 */
  const resetForm = () => {
    formData.value = {
      id: '',
      orderCode: '',
      type: '',
      code: '',
      specification: '',
      materialCheck: '',
      issuMaterials: '',
      remark: '',
      materialStatus: ''
    }
    formRef.value?.resetFields()
  }

  const loading = ref(true) // 列表的加载中
  const list = ref([]) // 主列表的数据
  const total = ref(0) // 列表的总页数
  const dataAll = ref()

  const isDisabled = ref(true)  //控制确定按钮点击
  /** 查询列表cartonCodeNumList */
  const getList = async (id) => {
      // console.log(id)
    loading.value = true
    try {
      dataAll.value = await materialPickApi.materialPickNum(id)
      // console.log(dataAll.value.cartonCodeNumList)
      list.value = dataAll.value.cartonCodeNumList
      total.value = dataAll.value.total
      if(list.value){
        isDisabled.value = false
      }else {
        isDisabled.value = true
      }
    } finally {
      loading.value = false
    }
  }
</script>
